#include <iostream>
#include <string>
#include <fstream>

#include "contacts.pb.h"

const std::string path = "contacts.bin";

void print_contacts(contacts::contacts& my_contacts)
{
    for (size_t i = 0; i < my_contacts.peoples_size(); ++i)
    {
        std::cout << "联系人" << i + 1 << ":" << std::endl;
        const contacts::people_info& people =  my_contacts.peoples(i);
        std::cout << "联系人姓名: " << people.name() << std::endl;
        std::cout << "联系人年龄: " << people.age() << std::endl;
        for (size_t j = 0; j < people.phone_numbers_size(); ++j)
        {
            const phone::phone_info& jphone = people.phone_numbers(j);
            std::cout << "联系人电话: " << jphone.number() << ' ';
            std::cout << "电话类型: " << phone::phone_info::phone_type_Name(jphone.type()) << std::endl;
        }
        if (people.has_data() && people.data().Is<contacts::Address>())
        {
            contacts::Address address;
            people.data().UnpackTo(&address);
            if (!address.home_address().empty())
            {
                std::cout << "联系人家庭地址: "; 
                std::cout << address.home_address() << std::endl;
            }
            if (!address.unit_address().empty())
            {
                std::cout << "联系人单位地址: "; 
                std::cout << address.home_address() << std::endl;
            }
        }
        int other_contact = people.other_contact_case();
        if (other_contact == contacts::people_info::OtherContactCase::kQq)
            std::cout << "联系人QQ: " << people.qq() << std::endl;
        else if (other_contact == contacts::people_info::OtherContactCase::kWechat)
            std::cout << "联系人wechat: " << people.wechat() << std::endl;
        else if (other_contact == contacts::people_info::OtherContactCase::OTHER_CONTACT_NOT_SET)
            ;
        if (people.remark_size() != 0)
        {
            std::cout << "备注信息: " << std::endl;
            for (auto it = people.remark().cbegin(); it != people.remark().cend(); ++it)
                std::cout << it->first << ":" << it->second << std::endl;
        }
        else
            ;
        // 获取打印未知字段
        const google::protobuf::Reflection* reflection = contacts::people_info::GetReflection();
        const google::protobuf::UnknownFieldSet& set = reflection->GetUnknownFields(people);
        for (size_t j = 0; j < set.field_count(); ++j)
        {
            const google::protobuf::UnknownField& unkonw_file = set.field(j);
            std::cout << "未知字段" << j + 1 << ": ";
            std::cout << "字段编号: " << unkonw_file.number() << " 值: ";
            switch (unkonw_file.type())
            {
            case google::protobuf::UnknownField::Type::TYPE_VARINT:
                std::cout << unkonw_file.varint() << std::endl;
                break;
            case google::protobuf::UnknownField::Type::TYPE_FIXED32:
                std::cout << unkonw_file.fixed64() << std::endl;
                break;
            case google::protobuf::UnknownField::Type::TYPE_FIXED64:
                std::cout << unkonw_file.fixed64() << std::endl;
                break;
            case google::protobuf::UnknownField::Type::TYPE_LENGTH_DELIMITED:
                std::cout << unkonw_file.length_delimited() << std::endl;
                break;
            // case google::protobuf::UnknownField::Type::TYPE_GROUP:
            //     std::cout << unkonw_file.group() << std::endl;
            //     break;
            default:
                break;
            }
        }
    }
}

int main()
{
    contacts::contacts my_contacts;
    //读取本地已存在的联系人文件
    std::ifstream fin(path.c_str(), std::ifstream::in | std::ifstream::binary);
    if (!fin)
        std::cout << path << " not find, create new file" << std::endl;
    else
    {
        if (!fin.is_open())
            std::cout << path << " open err" << std::endl;
        
        if (!my_contacts.ParseFromIstream(&fin))
        {
            std::cout << "parse err" << std::endl;
            fin.close();
            return -1;
        }
        fin.close();
    }
    print_contacts(my_contacts);
    return 0;
}